﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entity;

namespace Web.admin.InnerManage.Participants
{
    public partial class addGrop : System.Web.UI.Page
    {
        StudentService studentService = new StudentService();
        ParticipantService participantService = new ParticipantService();
        GroupsService groupsService = new GroupsService();
        ContestService contestService = new ContestService();
        ClassService classService = new ClassService();
        AcademyService academyService = new AcademyService();
        CompetitionService competitionService = new CompetitionService();
        static string GroupName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string contestId = Request["ContestId"];
                if (String.IsNullOrEmpty(contestId))
                    return;
               BoContests contest = contestService.getContest(contestId) ;

               BoCompetitions competi = competitionService.getCompetition(contest.CON_COMPETITIONS);

                if (competi.COM_FORM == 0)
                {

                    Response.Write("<script>alert('此竞赛不分小组');window.location.href='../InnerPar.aspx?id="+contestId+"'; </script>");
                    return;
                }

                List<BoContests> contests = new List<BoContests>();
                contests.Add(contest);
                ContestsList.DataSource = contests;
                ContestsList.DataTextField = "CON_NAME";
                ContestsList.DataValueField = "CON_ID";
                ContestsList.DataBind();

                BoCompetitions compe = competitionService.getCompetition(contest.CON_COMPETITIONS);
                IsSpecialtyList.SelectedValue =Convert.ToString(compe.COM_SPECIALTY);
                string operation = Request["OPERATION"];

                if (operation == null)
                {
                    return;
                }
                string compId = Request["id"];
                if (compId == null)
                {
                    return;
                }
                if (operation == "Modify")
                {
                    ModifyInner(compId, contestId);
                }
                else if (operation == "Delete")
                {
                    DeleteInner(compId);
                }
            }
        }

        /// <summary>
        /// 修改小组
        /// </summary>
        /// <param name="groId">小组id</param>
        /// <param name="conid">竞赛id</param>
        protected void ModifyInner(string groId,string conid)
        {
            //BoCompetitions comp = competitionService.getCompetition(compId);
            BoGroups gro = groupsService.getGroups(groId);   
            GroId.Value = gro.GRO_ID;
            TextGro.Text = gro.GRO_NAME;
            teaBox.Text = gro.GRO_TEACHER;
            GroupName = gro.GRO_NAME;

            ContestsList.SelectedValue = gro.GRO_CONTESTS;
            ContestsList.DataSource = contestService.getAllContests();
            ContestsList.DataTextField = "CON_NAME";
            ContestsList.DataValueField = "CON_ID";
            ContestsList.DataBind();

            foreach (ListItem item in IsSpecialtyList.Items)
            {
                if (gro.GRO_SPECIALTY.ToString() == item.Value)
                    item.Selected = true;
                else
                    item.Selected = false;
            }
        }

        /// <summary>
        /// 根据小组id删除小组
        /// </summary>
        /// <param name="groId">小组id</param>
        protected void DeleteInner(string groId)
        {
            BoGroups gro = groupsService.getGroups(groId);
            groupsService.DeleteGroups(gro);
            Response.Redirect("../InnerPar.aspx?id=" + ContestsList.SelectedValue);

        }

        /// <summary>
        /// 保存按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            BoGroups gro = new BoGroups();
            gro.GRO_NAME = TextGro.Text;
            gro.GRO_CONTESTS = ContestsList.SelectedValue;
            gro.GRO_TEACHER = teaBox.Text;
            gro.GRO_SPECIALTY = IsSpecialtyList.SelectedValue == "1" ? 1 : 0;
            if (string.IsNullOrEmpty(GroId.Value.Trim()))
            {
                if (groupsService.CheckGroupName(TextGro.Text.Trim(), Request.QueryString["ContestId"]))
                {
                    Response.Write("<script>alert('该竞赛下已有该小组名称，不允许重复')</script>");
                    return;
                }
                groupsService.CreateGroup(gro);
            }
            else
            {
                if (TextGro.Text != GroupName)
                {
                    if (groupsService.CheckGroupName(TextGro.Text.Trim(), Request.QueryString["ContestId"]))
                    {
                        Response.Write("<script>alert('该竞赛下已有该小组名称，不允许重复')</script>");
                        return;
                    }
                }
                gro.GRO_ID = GroId.Value;
                groupsService.UpdateGroups(gro);
            }
            Response.Redirect("../InnerPar.aspx?id=" + ContestsList.SelectedValue);

        }
        protected void ContestsList_SelectedIndexChanged(object sender, EventArgs e)
        {

            BoGroups gro = groupsService.getGroups(GroId.Value);
            BoContests con = contestService.getContest(ContestsList.SelectedValue);
            BoCompetitions com = competitionService.getCompetition(con.CON_COMPETITIONS);
            
            if (com.COM_TYPE == 0)
            {
                IsSpecialtyList.Enabled = false;
            }
            else
            {
                IsSpecialtyList.Enabled = true;
            }
        }
    }
}